<?php  //if (!defined('BASEPATH')) exit('No direct script access allowed');
// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * Treacle Config Class file
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id $
 * @package libraries
 */
/**
 * 系统配置静态类
 *
 * @package libraries
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
final class Configure
{
	private static $is_loaded 	= array();
	private static $main_conf	= array();
	/**
	 * 初始化函数
	 *
	 * @access   public
	 * @return  boolean  if the file was successfully loaded or not
	 */
	public static function initialize()
	{
		if ( ! file_exists(APPPATH.'config/config'.EXT))
		{
			throw new TSystemException('configuration_file_not_exist');
		}
		require(APPPATH.'config/config'.EXT);
		if ( ! isset($config) OR ! is_array($config))
		{
			throw new TSystemException('configuration_file_not_appear');
		}
		self::$main_conf[0] =& $config;
		unset($config);
	}
	/**
	 * 加载一个config文件
	 * 
	 * @access	public
	 * @param	string	the config file name
	 * @return	boolean	if the file was loaded correctly
	 */
	public static function load($file = '', $use_sections = TRUE)
	{
		//$file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
		if (in_array($file, self::$is_loaded, TRUE))
		{
			return TRUE;
		}
		if ( ! file_exists(APPPATH.'config/'.$file.EXT))
		{
			if ($fail_gracefully === TRUE)
			{
				throw new TConfigurationException('configuration_file_not_exist',$file.EXT);
			}
		}
		include(APPPATH.'config/'.$file.EXT);
		if (!isset($config) OR ! is_array($config))
		{
			throw new TConfigurationException('configuration_file_not_appear',$file.EXT);
		}
		if ($use_sections === TRUE)
		{
			if (isset(self::$main_conf[$file]))
			{
				self::$main_conf[$file] = array_merge(self::$main_conf[$file], $config);
			}
			else
			{
				self::$main_conf[$file] = $config;
			}
		}
		else
		{
			self::$main_conf[0] = array_merge(self::$main_conf[0], $config);
		}
		self::$is_loaded[] = $file;
		unset($config);
	}
	/**
	 * 获取一个config项目
	 *
	 * @access	public
	 * @param	string	the config item name
	 * @param	string	the index name
	 * @return	string
	 */
	public static function item($item, $index = 0)
	{
			if (!isset(self::$main_conf[$index][$item]))
			{
				return FALSE;
			}
			if (!isset(self::$main_conf[$index][$item]))
			{
				return FALSE;
			}
	}
	/**
	 * 返回全部config数组
	 *
	 * @return unknown
	 */
	public static function item_array($index=0)
	{
		return isset(self::$main_conf[$index])?self::$main_conf[$index]:array();
	}
	/**
	 * _slash_item
	 *
	 * @param string $item
	 * @return array
	 */
	private static function _slash_item($item,$index=0)
	{
		if ( ! isset(self::$main_conf[$index][$item]))
		{
			return FALSE;
		}
		$pref = self::$main_conf[$index][$item];
		if ($pref != '')
		{
			if (ereg("/$", $pref) === FALSE)
			{
				$pref .= '/';
			}
		}
		return $pref;
	}
	/**
	 * 站点路径函数,用来构建uri方式的路径
	 * @access	public
	 * @param	string	URI string
	 * @return	string
	 */
	public static function site_url($uri = '')
	{
		if (is_array($uri))
		{
			$uri = implode('/', $uri);
		}
		if ($uri == '')
		{
			return self::_slash_item('base_url').self::item('first_page');
		}
		else
		{
			$first_page = (self::item('first_page')!='')?self::item('first_page')."/":'';
			return self::item('base_url').$first_page.preg_replace("|^/*(.+?)/*$|", "\\1", $uri);
		}
	}
	/**
	 * 网站的根路径
	 * @access	public
	 * @return	string
	 */
	public static function base_url()
	{
		$x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
		return $this->_slash_item('base_url').end($x).'/';
	}
	/**
	 * 设置一个配置项
	 *
	 * @access	public
	 * @param	string	the config item key
	 * @param	string	the config item value
	 * @return	void
	 */
	public function set_item($item, $value)
	{
		self::$main_conf[0][$item] = $value;
	}
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Wed Nov 07 23:55:06 CST 2007
+--------------------------------------------------------------------------
*/
?>